package com.zzq.guli.service.sms.controller;

import com.zzq.guli.service.base.result.R;
import com.zzq.guli.service.base.util.RandomUtils;
import com.zzq.guli.service.sms.service.SmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.PermitAll;
import java.util.concurrent.TimeUnit;


//@CrossOrigin
@RequestMapping("/api/user/sms")
@RestController
public class SmsController {


    @Autowired
    StringRedisTemplate redisTemplate;

    @Autowired
    SmsService smsService;

    //调用阿里云短信服务发短信
    @GetMapping("getCode/{phoneNumber}")
    public R getCheckCode(@PathVariable String phoneNumber) throws Exception {

        System.out.println("准备发短信了");

        String checkCode = RandomUtils.getFourBitRandom();
        //向阿里云发送短信后
        String code=smsService.getCode(phoneNumber,checkCode);


        //把验证码存入redis
        redisTemplate.opsForValue().set("checkCode::"+checkCode,checkCode,60, TimeUnit.MINUTES);

        return R.ok().data("code",code).message("短信发送成功");
    }

    @GetMapping("test")
    public R test() throws Exception {

        System.out.println("准备发短信了");

        return R.ok();
    }

}
